package cn.iocoder.yudao.module.nmkj.controller.admin.useraccess;

import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.nmkj.controller.admin.useraccess.vo.*;
import cn.iocoder.yudao.module.nmkj.dal.dataobject.useraccess.UserAccessDO;
import cn.iocoder.yudao.module.nmkj.service.useraccess.UserAccessService;

@Tag(name = "管理后台 - 访客消息")
@RestController
@RequestMapping("/nmkj/user-access")
@Validated
public class UserAccessController {

    @Resource
    private UserAccessService userAccessService;

    @PostMapping("/create")
    @Operation(summary = "创建访客消息")
    @PreAuthorize("@ss.hasPermission('nmkj:user-access:create')")
    public CommonResult<Long> createUserAccess(@Valid @RequestBody UserAccessSaveReqVO createReqVO) {
        //所有的访客消息，一律设置为未审核状态
        createReqVO.setId(null);
        createReqVO.setStatus(0);
        return success(userAccessService.createUserAccess(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新访客消息")
    @PreAuthorize("@ss.hasPermission('nmkj:user-access:update')")
    public CommonResult<Boolean> updateUserAccess(@Valid @RequestBody UserAccessSaveReqVO updateReqVO) {
        userAccessService.updateUserAccess(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除访客消息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nmkj:user-access:delete')")
    public CommonResult<Boolean> deleteUserAccess(@RequestParam("id") Long id) {
        userAccessService.deleteUserAccess(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得访客消息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nmkj:user-access:query')")
    public CommonResult<UserAccessRespVO> getUserAccess(@RequestParam("id") Long id) {
        UserAccessDO userAccess = userAccessService.getUserAccess(id);
        return success(BeanUtils.toBean(userAccess, UserAccessRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得访客消息分页")
    @PreAuthorize("@ss.hasPermission('nmkj:user-access:query')")
    public CommonResult<PageResult<UserAccessRespVO>> getUserAccessPage(@Valid UserAccessPageReqVO pageReqVO) {
        PageResult<UserAccessDO> pageResult = userAccessService.getUserAccessPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserAccessRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出访客消息 Excel")
    @PreAuthorize("@ss.hasPermission('nmkj:user-access:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserAccessExcel(@Valid UserAccessPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserAccessDO> list = userAccessService.getUserAccessPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "访客消息.xls", "数据", UserAccessRespVO.class,
                        BeanUtils.toBean(list, UserAccessRespVO.class));
    }

}